Your goal is to create this plot using plotly.

  1. Download https://michaelgastner.com/DAVisR_data/life_quality.csv.

  2. Make a scatterplot where:

  • The x-coordinate is the GDP per capita.
  • The y-coordinate is the life expectancy.
  • mode = "markers"
  1. Set the colors of each point to indicate which continent it belongs to.
  • Make sure you have two separate parameters color and colors:

    • color should specify which column or variable to map to the color of each point.
    • colors should specify which colors or palette to use.
  • You may use this palette for coloring continents:

    colors <- c("#e41a1c", '#377eb8', '#4daf4a', '#984ea3', '#ff7f00')
  1. Set the size of each bubble to indicate the population.
  • You will see the following warning Warning: line.width does not currently support multiple values.

  • You may consider using fill = ~'' in plot_ly(); this code assigns an empty string to fill.

  • Unfortunately, as of today, plotly does not support the size legend. However, some people seem to have found a way around by creating a new plot just for legend and combine it with the original bubble chart using subplot function. You can try it once you are done with the rest of exercises.

  1. Assign texts to each point that will show when you hover over it.
  • The text should contain the country name, GDP per capita, life expectancy, and population.
  • Remember to add line breaks.
  • For GDP per capita and life expectancy, round the numbers to 2 digits.
  • Place a dollar sign in front of GDP per capita.
  1. Add labels to the plot using layout()
  • Change the axis labels and give the plot a title.

  • Add an appropriate legend.

  • Give credit to the World Bank as a source. To accomplish this, you may consider using the code below:

    annotations = list(
      x = 1.2, y = -0.1,
      text = "Source: World Bank",
      showarrow = F, xref = "paper", yref = "paper",
      xanchor = "right", yanchor = "auto", xshift = 0, yshift = 0,
      font = list(size = 12)
    )
  1. Fine-tune and complete the plot.
  • Make sure the x-axis is log-transformed.

  • To transform into a logarithmic axis, you should use this code:

    xaxis = list(
      type = "log", 
      showgrid = TRUE, 
      tickmode = "linear"
    )
  • Make sure the sizes of the symbol in the legend are constant.

  • Add a thin white border around the points to make it easier to distinguish overlapping points.

  • Feel free to make more adjustments if you think they will improve the quality of the plot.

Thank you!